package com.example.j202411.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.j202411.R;
import com.example.j202411.Model.PetEntity;
import com.example.j202411.views.Activity.PetsDetailsActivity;
import com.google.gson.Gson;


import java.util.List;

public class PetAdapter extends RecyclerView.Adapter<PetAdapter.ViewHolder> {
    private List<PetEntity.Result.ListDate> mlist;
    private Context context;

    public PetAdapter(List<PetEntity.Result.ListDate> list, Context context) {
        this.mlist = list;
        this.context = context;
    }


    public void updateData(List<PetEntity.Result.ListDate> list) {
        mlist.clear();
        this.mlist = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public PetAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_pet_list, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PetAdapter.ViewHolder holder, int position) {
        PetEntity.Result.ListDate data = mlist.get(position);

        if (data != null) {
            holder.title.setText(data.getName());
            Glide.with(context)
                    .load(data.getCoverURL())
                    .into(holder.imageView1);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent =new Intent(context, PetsDetailsActivity.class);
                    Gson gson =new Gson();
                    intent.putExtra("data",data);
                    context.startActivity(intent);
                }
            });
        }


    }

    @Override
    public int getItemCount() {
        return mlist==null?0:mlist.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ImageView imageView1;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.name);
            imageView1 = itemView.findViewById(R.id.imageView);
        }
    }
}
